#==============================================================================
# ■ Window_EquipRight
#------------------------------------------------------------------------------
# 　装备画面、显示角色现在装备的物品的窗口。
#==============================================================================

class Window_EquipRight < Window_Selectable
  #--------------------------------------------------------------------------
  # ● 初始化对像
  #     actor : 角色
  #--------------------------------------------------------------------------
  def initialize(actor)
    super(240, 0, 336, 240)
    self.contents = Bitmap.new(width - 32, height - 32)
    @bscheck = -1
    # 改为4槽且默认可用：取消读取闭锁信息
    # if $game_variables[285]!=0
    #   @bscheck=($game_actors[$game_variables[1]+1].get_buff_time(10)-1)/5
    # end
    @actor = actor
    refresh
    self.index = @bscheck + 1
  end
  #--------------------------------------------------------------------------
  # ● 获取物品
  #--------------------------------------------------------------------------
  def item
    return @data[self.index]
  end
  def draw_item_name(item, x, y ,index)
    if item == nil
      return
    end
    
    bitmap = RPG::Cache.icon(item.icon_name)
    # 等比例缩小以防重叠：在内容区均分为4格
    slot_w = (self.contents.width / 4).floor
    margin = 6
    inner_w = slot_w - margin - 6
    # 基于宽度的初始缩放（不放大只缩小）
    scale = [inner_w, 86].min.to_f / 86.0
    # 名称与总高度的估算
    name_pad = 2
    name_font_size = [[(16 * scale).to_i, 9].max, 16].min
    name_h = [[(18 * scale).to_i, name_font_size + 4].max, 18].min
    dest_w = (86 * scale).to_i
    dest_h = (64 * scale).to_i
    total_h = dest_h + name_pad + name_h
    # 若总高度超过内容高度，按比例收缩并留安全边距
    max_total_h = self.contents.height - 8
    if total_h > max_total_h
      factor = (max_total_h.to_f / total_h) * 0.95
      scale = (scale * factor)
      name_font_size = [[(16 * scale).to_i, 9].max, 16].min
      name_h = [[(18 * scale).to_i, name_font_size + 4].max, 18].min
      dest_w = (86 * scale).to_i
      dest_h = (64 * scale).to_i
      total_h = dest_h + name_pad + name_h
    end
    # 将每个槽位在该格内水平居中，整排在窗口内垂直居中（包含名称区域）
    dest_x = (slot_w * index) + (slot_w - dest_w) / 2
    dest_y = (self.contents.height - total_h) / 2
    self.contents.stretch_blt(Rect.new(dest_x, dest_y, dest_w, dest_h), bitmap, Rect.new(0, 0, 86, 64), opacity)

    self.contents.font.color = normal_color
    self.contents.font.size=16
    if $game_variables[181][item.id]!=nil && $game_variables[181][item.id]!=0 && !@check_cn.include?(item.id)
      @check_cn.push(item.id)
      if $game_variables[181][item.id]==-1
        str="开启"
      else
        str=$game_variables[181][item.id].to_s
      end
      self.contents.font.color = text_color(12)
      # 基于图标位置绘制叠字
      ox = dest_x + 7
      oy = dest_y - 11
      self.contents.draw_text(ox, oy+1, 32, 32, str,2)
      self.contents.draw_text(ox, oy-1, 32, 32, str,2)
      self.contents.draw_text(ox+1, oy, 32, 32, str,2)
      self.contents.draw_text(ox-1, oy, 32, 32, str,2)
      self.contents.font.color = normal_color
      self.contents.draw_text(ox, oy, 32, 32, str,2)
    end
    if item.id==39 && $game_switches[142]
      self.contents.font.color = text_color(12)
      ox = dest_x - 23
      oy = dest_y - 11
      self.contents.draw_text(ox, oy+1, 62, 32, "冷却中",2)
      self.contents.draw_text(ox, oy-1, 62, 32, "冷却中",2)
      self.contents.draw_text(ox+1, oy, 62, 32, "冷却中",2)
      self.contents.draw_text(ox-1, oy, 62, 32, "冷却中",2)
      self.contents.font.color = normal_color
      self.contents.draw_text(ox, oy, 62, 32, "冷却中",2)
    end
    if item.id==74 && $game_switches[159]
      str="开启"
      self.contents.font.color = text_color(12)
      ox = dest_x - 40
      oy = dest_y - 11
      self.contents.draw_text(ox, oy+1, 32, 32, str,2)
      self.contents.draw_text(ox, oy-1, 32, 32, str,2)
      self.contents.draw_text(ox+1, oy, 32, 32, str,2)
      self.contents.draw_text(ox-1, oy, 32, 32, str,2)
      self.contents.font.color = normal_color
      self.contents.draw_text(ox, oy, 32, 32, str,2)
    end
    
    # 改为4槽且默认可用：取消闭锁覆盖
    # if $game_variables[285]!=0 && index<=@bscheck
    #   bitmap = RPG::Cache.icon("装备-闭锁")
    #   self.contents.blt(x-43, y-50 , bitmap, Rect.new(0, 0, 86, 64), opacity)
    # end
    
    str=item.name.split(":")[0]
    text=TextStream.new
    text.open("Info/Equip/"+str)
    color=text.get_text(1)
    color.sub!(/\[([0-9]+)\]/, "")
    text.close
    self.contents.font.color=text_color($1.to_i)
    # 将装备名称绘制在图标正下方，按比例缩小并居中
    self.contents.font.size = name_font_size
    name_x = slot_w * index
    name_y = dest_y + dest_h + name_pad
    self.contents.draw_text(name_x, name_y, slot_w, name_h, str,1)
  end
  #--------------------------------------------------------------------------
  # ● 更新光标举行
  #--------------------------------------------------------------------------
  def update
   # super
    # 可以移动光标的情况下
    if self.active and @item_max > 0 and @index >= 0
      # 改为四槽一排：禁用上下移动
      # if Input.repeat?(Input::DOWN)
      # end
      # if Input.repeat?(Input::UP)
      # end
      # 方向键右被按下的情况下
      if Input.repeat?(Input::RIGHT)
        # 循环导航：如果在最右边，则跳到最左边
        $game_system.se_play($data_system.cursor_se)
        if @index < @item_max - 1
          # 光标向右移动
          @index += 1
        else
          # 在最右边，循环到最左边
          @index = 0
        end
      end
      # 方向键左被按下的情况下
      if Input.repeat?(Input::LEFT)
        # 循环导航：如果在最左边，则跳到最右边
        $game_system.se_play($data_system.cursor_se)
        if @index > 0
          # 光标向左移动
          @index -= 1
        else
          # 在最左边，循环到最右边
          @index = @item_max - 1
        end
      end
    end
    # 改为4槽且默认可用：取消闭锁限制
    # if @index<@bscheck+1
    #   @index=@bscheck+1
    # end
    # 刷新帮助文本 (update_help 定义了继承目标)
    if self.active and @help_window != nil
      update_help
    end
    # 刷新光标矩形
    update_cursor_rect
  end
  def update_cursor_rect
    # 光标位置不满 0 的情况下
    if @index < 0
      self.cursor_rect.empty
      return
    end
    # 更新光标矩形：包含图标与文字的总高度，且不与相邻装备重叠
    slot_w = (self.contents.width / 4).floor
    margin = 6
    inner_w = slot_w - margin - 6
    scale = [inner_w, 86].min.to_f / 86.0
    name_pad = 2
    name_font_size = [[(16 * scale).to_i, 9].max, 16].min
    name_h = [[(18 * scale).to_i, name_font_size + 4].max, 18].min
    dest_w = (86 * scale).to_i
    dest_h = (64 * scale).to_i
    total_h = dest_h + name_pad + name_h
    max_total_h = self.contents.height - 8
    if total_h > max_total_h
      factor = (max_total_h.to_f / total_h) * 0.95
      scale = (scale * factor)
      name_font_size = [[(16 * scale).to_i, 9].max, 16].min
      name_h = [[(18 * scale).to_i, name_font_size + 4].max, 18].min
      dest_w = (86 * scale).to_i
      dest_h = (64 * scale).to_i
      total_h = dest_h + name_pad + name_h
    end
    x = @index * slot_w + margin / 2
    y = (self.contents.height - total_h) / 2 - 2
    w = slot_w - margin
    h = total_h + 4
    self.cursor_rect.set(x, y, w, h)
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    @data = []
    # 改为固定4个槽位
    for j in 0...4
      i=@actor.equip_id[j]
      if i==nil
        @data.push(nil)
      else
        @data.push($data_weapons[i])
      end
    end
    @item_max = @data.size
    self.contents.font.color = system_color
    @check_cn=[]

    # 装备槽类型标签数组
    equip_slot_types = ["武器", "防具", "饰品", "饰品"]
    
    # 绘制装备槽类型标签（在装备槽上方）
    slot_w = (self.contents.width / 4).floor
    for i in 0...4
      type_label = equip_slot_types[i]
      # 设置字体为红色
      self.contents.font.color = text_color(2)  # 红色
      self.contents.font.size = 18
      # 计算类型标签位置：在每个装备槽上方居中显示
      label_x = slot_w * i
      label_y = 10  # 距离顶部的位置
      self.contents.draw_text(label_x, label_y, slot_w, 32, type_label, 1)
    end

    # 改为单行4格，横向排列，居中显示
    for i in 0...4
      ritem=@data[i]
      # 传入的 x,y 仅用于兼容旧接口，这里主要按 index 在 draw_item_name 内部定位
      draw_item_name(ritem, 0, self.contents.height/2, i)
    end
  end
  #--------------------------------------------------------------------------
  # ● 刷新帮助文本
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_text(self.item == nil ? "" : self.item.id)
  end
end
